package top.yangbuyi.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.Role;
import top.yangbuyi.dto.RoleDto;
import top.yangbuyi.mapper.RoleMapper;
import top.yangbuyi.service.impl.RoleService;
import top.yangbuyi.vo.DataGridView;

/**
 * ClassName: Loginfo
 * Description: 杨不易网站 :www.yangbuyi.top
 * date: 2020/9/7
 *
 * @author TeouBle
 * @author yangbuyi
 * @since JDK 1.8
 **/

@Service
public class RoleServiceImpl implements RoleService {

	  @Autowired
	  private RoleMapper roleMapper;

	  /**
	   * 分页查询角色
	   *
	   * @param roleDto
	   * @return
	   */
	  @Override
	  public DataGridView listRolePage(RoleDto roleDto) {
			Page<Role> page = new Page<>(roleDto.getPageNum(), roleDto.getPageSize());
			QueryWrapper<Role> qw = new QueryWrapper<>();
			qw.like(StringUtils.isNotBlank(roleDto.getRoleName()), Role.COL_ROLE_NAME, roleDto.getRoleName());
			qw.like(StringUtils.isNotBlank(roleDto.getRoleCode()), Role.COL_ROLE_CODE, roleDto.getRoleCode());
			qw.eq(StringUtils.isNotBlank(roleDto.getStatus()), Role.COL_STATUS, roleDto.getStatus());
			qw.ge(null != roleDto.getBeginTime(), Role.COL_CREATE_TIME, roleDto.getBeginTime());
			qw.le(null != roleDto.getEndTime(), Role.COL_CREATE_TIME, roleDto.getEndTime());
			qw.orderByAsc(Role.COL_ROLE_SORT);
			this.roleMapper.selectPage(page, qw);
			return new DataGridView(page.getTotal(), page.getRecords());
	  }

	  /**
	   * 查询所有可用角色
	   *
	   * @return
	   */
	  @Override
	  public List<Role> listAllRoles() {
			QueryWrapper<Role> qw = new QueryWrapper<>();
			qw.eq(Role.COL_STATUS, Constants.STATUS_TRUE);
			qw.orderByAsc(Role.COL_ROLE_SORT);
			return this.roleMapper.selectList(qw);
	  }

	  /**
	   * 根据ID查询角色
	   *
	   * @param roleId
	   * @return
	   */
	  @Override
	  public Role getOne(Long roleId) {
			return this.roleMapper.selectById(roleId);
	  }

	  /**
	   * 添加一个角色
	   *
	   * @param roleDto
	   * @return
	   */
	  @Override
	  public int addRole(RoleDto roleDto) {
			Role role = new Role();
			BeanUtil.copyProperties(roleDto, role);
			// 设置创建者
			role.setCreateBy(roleDto.getSimpleUser().getUserName());
			// 设置创建时间
			role.setCreateTime(DateUtil.date());
			return this.roleMapper.insert(role);
	  }

	  /**
	   * 修改角色
	   *
	   * @param roleDto
	   * @return
	   */
	  @Override
	  public int updateRole(RoleDto roleDto) {
			Role role = new Role();
			BeanUtil.copyProperties(roleDto, role);
			// 设置修改者
			role.setCreateBy(roleDto.getSimpleUser().getUserName());
			// 设置修改时间
			role.setUpdateTime(DateUtil.date());
			return this.roleMapper.updateById(role);
	  }

	  /**
	   * 根据角色ID删除角色
	   *
	   * @param roleIds
	   * @return
	   * @RBAC思路：删除菜单数据也要删除与之关联的中间表数据
	   */
	  @Override
	  public int deleteRoleByIds(Long[] roleIds) {
			List<Long> idList = Arrays.asList(roleIds);
			if (idList != null && !idList.isEmpty()) {
				  // 根据角色id删除sys_role_menu表数据
				  this.roleMapper.deleteRoleIdByMenu(idList);
				  // 根据角色id删除sys_role_user表数据
				  this.roleMapper.deleteRoleIdByUser(idList);
				  // 删除角色
				  return this.roleMapper.deleteBatchIds(idList);
			}
			return 0;
	  }

	  /**
	   * @param roleId
	   * @param menuIds
	   * @功能描述:保存角色和菜单关系
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/7 21:29
	   */
	  @Override
	  public void saveRoleMenu(Long roleId, Long[] menuIds) {
			// 根据角色ID删除role_menu数据
			this.roleMapper.deleteRoleIdByMenu(Arrays.asList(roleId));
			for (Long menuId : menuIds) {
				  this.roleMapper.saveRoleMenu(roleId,menuId);
			}
	  }

	  /**
	   * 根据用户ID查询用户拥有的角色IDS
	   * @param userId
	   * @return
	   */
	  @Override
	  public List<Long> getRoleIdsByUserId(Long userId) {
			if(null==userId){
				  return Collections.EMPTY_LIST;
			}
			return this.roleMapper.selectRoleIdsByUserId(userId);
	  }

	  @Override
	  public void saveRoleUser(Long userId, Long[] roleIds) {
			//根据用户ID先删除sys_role_user里面原来的数据
			this.roleMapper.deleteRoleUserByUserIds(Arrays.asList(userId));
			for (Long roleId : roleIds) {
				  this.roleMapper.saveRoleUser(userId,roleId);
			}
	  }

}
